
function deepCopy(val, walkedMap = new Map()) {
    const isPlainObj = Object.prototype.toString.call(val) === '[object Object]'
    const isArray = Array.isArray(val)
    if (!isPlainObj && !isArray) {
     return val
    } else if (isArray) {
     return val.map((el) => deepCopy(el, walkedMap))
    }
    // 避免循环引用 导致递归无法终止
    const cached = walkedMap.get(val)
    if (cached) {
     return cached
    }
 
    
    const cloned = Object.fromEntries(Object.entries(val).map(([ekey, eval]) => ([ekey, deepCopy(eval, walkedMap)])))
    walkedMap.set(val, cloned)
    
    return cloned
 }
 
 
 var fav = {name: 'football'}
 var obj = { fav, colors: [{name: 'red'}, {name: 'blue'}], num: 10, change() { console.log('cc')}, friend: { fav } }
 var cloned = deepCopy(obj)
 console.log(cloned, obj);
 